package iceberg;

import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.util.Benchmark;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public final class Main {

	private Main() {
	}

	public static void main(String[] args) throws IOException, ClassNotFoundException, UnsupportedLookAndFeelException, IllegalAccessException, InstantiationException {

		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

		List<String> arguments = new ArrayList<String>();
		arguments.add(Config.getTestProjectLocation());
		arguments.add(Config.getOutputFormat());
		arguments.add(Config.getRuleSetLocation());

		LoggerFactory.getLogger(Main.class); // Initialize logging

		PrintStream normalOut = System.out;
		File tempFile = new File(Config.getTempFile());
		PrintStream fileOut = new PrintStream(new FileOutputStream(tempFile));

		System.setOut(fileOut);
		PMD.main(arguments.toArray(new String[arguments.size()]));

		System.setOut(normalOut);

		fileOut.flush();
		fileOut.close();

		Desktop.getDesktop().open(tempFile);
		LoggerFactory.getLogger("report").info(Benchmark.report());

	}
}
